package com.kgis.fm.business.management.service.support;






public class FindInstead {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String aa = "IFOPEN =1 AND STATUS_C= 0 AND JOB_STATUS ='竣工' AND STATE ='投运' AND PRESS_IN_O ='MPB'";
		String aa1 = "JOB_STATUS = '设计'  AND STATE ='投运' AND PRESS_O IN ('MPA','MPB') AND AGUG IN (2,3,4)";
		FindInstead findnnstead = new FindInstead();
		
		
		
		String result = findnnstead.reOgString(aa);
		System.out.println(result);
		
		/*String string = "IFOPEN = 1  AND AGUG IN (2,3,4) AND JOB_STATUS = '竣工'  AND PRESS_O IN ( 'HPA','HPB')";
		String tt="IFOPEN = 1  AND AGUG IN (2,3,4) AND PRESS_O IN ( 'HPA','HPB')";
		String ss = "AGUG IN (2,3,4) AND IFOPEN = 1 ";
		String yy="AGUG IN (2,3,4) AND PRESS_O IN ('HPA','HPB') AND IFOPEN = 1";
		String ee="IFOPEN = 1";
		String uu="AGUG IN (2,3,4)";
		String ff="";*/
		/*System.out.println(findnnstead.reOgString(string+"AND"));
		System.out.println(findnnstead.reOgString(tt));
		System.out.println(findnnstead.reOgString(ss));
		System.out.println(findnnstead.reOgString(yy));
		System.out.println(findnnstead.reOgString(ee));
		System.out.println(findnnstead.reOgString(uu));
		System.out.println(findnnstead.reOgString(ff));*/
		
		
		
		
	}
	
	public String reOgString(String instr){
		int in_cur = 1;
		int in_next = 0;
		String andstr = "";
		String reandstr = "";
		String laststr = "";
		String laststrsub;
		
		if(instr==null || instr.equals("")){
			return "";
		}
		//补上一个AND，根据AND取出每个AND的字符串，不然最后一次的就没了
		else{
			instr = instr+"AND";
		}
		
		//取出每一个and的字符串
		while((in_next=instr.indexOf("AND", in_cur))!=-1){
			//字符是从0开始计算的，in_cur默认为1，因此第一次截取需要-1，但是为保证and后面不管
			//有没空格都正确，截取的字符是从上一次找到的and的位置加上and的本身长度3开始截取
			//所以第二次截取的字符串cur就不能减1了，减1就会多了and中的d字符了
			if(in_cur==1){
				andstr = instr.substring(in_cur-1, in_next);
			}else{
				andstr = instr.substring(in_cur, in_next);
			}
			
			in_cur = in_next + 3;
			//System.out.println(andstr);
			
			// 判断是不是有IN
			if(andstr.indexOf("IN", 1)!=-1){
			   //取出in对应的字段 	
			   String fieldvalue = andstr.substring(0, andstr.indexOf("IN")-1).trim(); 
			   //取出in里的字符
			   String invalue = andstr.substring(andstr.indexOf('(')+1, andstr.indexOf(')')).trim();
			   
			   //将in里的字符分割成数组
			   String[] inarray =  invalue.split(","); 
			  //重新以or的形式拼装
			   for(int i=0;i<inarray.length;i++){
				   reandstr = reandstr+fieldvalue+"=="+inarray[i]+" or ";
			   }
			   laststrsub = "("+reandstr.substring(0, reandstr.lastIndexOf("or")-1)+")";//截掉最後一個or
			   reandstr = "";//循环完后重新置为空，否则会累加
			}else{
				laststrsub = andstr.replaceFirst("=", "==").replaceAll("OR", "or");
				
			}
			
			laststr=laststr+laststrsub+" and ";
		}
		laststr = laststr.substring(0, laststr.lastIndexOf("and")).trim();//去掉最后一个and
		
		//如果只有一个in的条件，则去掉括号
		if((laststr.indexOf("and")==-1) && (laststr.startsWith("(")) && (laststr.endsWith(")"))){
			laststr = laststr.substring(1, laststr.lastIndexOf(')'));
		}
		
		return laststr;
	}	

}
